package com.sj.purchase.modules.cost.web;

import com.sj.purchase.modules.cost.views.stock.InStockDetailsVO;
import com.sj.purchase.modules.cost.views.stock.OutStockDetailsVO;
import com.sj.purchase.modules.cost.views.stock.StockDetailsVO;
import com.sj.purchase.common.Result;
import com.sj.purchase.modules.cost.service.stock.StockDetailsService;
import com.sj.purchase.util.ExcelDownloadUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

import static com.sj.purchase.common.ResultGenerator.ok;

/**
 * 库存报表明细
 * Created by yangrd on 2017/7/25.
 */
@RestController
@RequestMapping("api/stock/report")
public class StockDetailsController {

    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static Function<Date, String> toDay = (date -> DateFormatUtils.format(date, DATE_FORMAT));

    @Autowired
    private StockDetailsService service;

    //http://192.168.2.25:8080/api/stock/report/stock-details?day=2017/06/23&sku=CD011202
    //今天的期末是明天的期初 TODO 时间需要留意
    //库存明细 查询条件 商品编码+日期
    @GetMapping("stock-details")
    public Result<Page<StockDetailsVO>> stockDetails(Date day, @RequestParam(defaultValue = "") String sku, Pageable pageable) {
        Pair<List<StockDetailsVO>, Long> pair = service.getStockDetailsAndTotal(day, sku + "%", pageable);
        return ok(new PageImpl<>(pair.getLeft(), pageable, pair.getRight()));
    }

    @GetMapping("stock-details/excel")
    public void exportStockDetailsExcel(Date day, String sku) {
        download(toDay.apply(day) + "sku:" + sku + "库存明细", StockDetailsVO.class, service.getStockDetailsAndTotal(day, sku, null).getLeft());
    }

    //http://192.168.2.25:8080/api/stock/report/in-stock-details?day=2017/06/21&sku=CD011202&orderNo=SRR1706210064
    //入库明细   入库单号 spu+日期+imei
    @GetMapping("in-stock-details")
    public Result<Page<InStockDetailsVO>> inStockDetails(Date day, String orderNo, String sku, @RequestParam(required = false) String imei, Pageable pageable) {
        Pair<List<InStockDetailsVO>, Long> pair = service.getInStockDetailsAndTotal(day, orderNo, sku, imei, pageable);
        return ok(new PageImpl<>(pair.getLeft(), pageable, pair.getRight()));
    }

    @GetMapping("in-stock-details/excel")
    public void exportInStockDetailsExcel(Date day, String orderNo, String sku, @RequestParam(required = false) String imei) {
        if (Objects.isNull(imei)) {
            download(toDay.apply(day) + "订单号:" + orderNo + "sku:" + sku + "入库明细", InStockDetailsVO.class, service.getInStockDetailsAndTotal(day, orderNo, sku, imei, null).getLeft());
        } else {
            download("串码:" + imei + "入库明细", InStockDetailsVO.class, service.getInStockDetailsAndTotal(day, orderNo, sku, imei, null).getLeft());
        }
    }

    //http://192.168.2.25:8080/api/stock/report/out-stock-details?day=2017/06/21&sku=AM241253
    //出库明细   spu+日期+imei
    @GetMapping("out-stock-details")
    public Result<Page<OutStockDetailsVO>> outStockDetails(Date day, String sku, @RequestParam(required = false) String imei, Pageable pageable) {
        Pair<List<OutStockDetailsVO>, Long> pair = service.getOutStockDetailsAndTotal(day, sku, imei, pageable);
        return ok(new PageImpl<>(pair.getLeft(), pageable, pair.getRight()));
    }

    @GetMapping("out-stock-details/excel")
    public void exportOutStockDetailsExcel(Date day, String sku, @RequestParam(required = false) String imei) {
        if (Objects.isNull(imei)) {
            download(toDay.apply(day) + "sku:" + sku + "出库明细", OutStockDetailsVO.class, service.getOutStockDetailsAndTotal(day, sku, imei, null).getLeft());
        } else {
            download("串码:" + imei + "出库明细", OutStockDetailsVO.class, service.getOutStockDetailsAndTotal(day, sku, imei, null).getLeft());
        }
    }

    private <T> void download(String name, Class<T> t, List<T> content) {
        ExcelDownloadUtil.easyPoi(name, t, content);
    }
}
